package com.hw.one.dam2.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hw.one.dam2.param.RegionSiteParam;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.common.SiteTypeEnum;
import com.hw.one.dam2.service.IRegionSiteService;
import com.hw.one.ext.util.UserUtil;

@RestController
@RequestMapping(value = "regionSite")
public class RegionSiteController extends BaseController{

	protected final Logger log = Logger.getLogger(this.getClass());

	@Resource
	private IRegionSiteService regionSiteService;

	@RequestMapping(value = "/findSiteInfoByRegionCode", method = RequestMethod.GET)
	public MsgResult findSiteInfoByRegionCode() {
		try {
			List<Map<String, Object>> SiteLists = regionSiteService.findSiteInfoByRegionCode("410100000");
			return MsgResult.okResult(SiteLists);
		} catch (Exception e) {
			log.error("无参查询", e);
			return MsgResult.exceptionResult();
		}
	}
	
	/**
	 * 根据输入的内容，返回地图上已有站点中含有输入内容的站点信息（主要包括经纬度）
	 * @author  sjd 
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "siteInfos/siteTypes", method = RequestMethod.POST)
	public MsgResult findSiteInfoBySiteTypes(HttpServletRequest request, HttpServletResponse response){
		//参数：站点名siteName，站点类型（多个“，”分割）siteTypeList
		Map<String, Object> params = wrapParams(request);
		String siteTypeList = (String)params.get("siteTypeList");
		String siteName = (String)params.get("siteName");
		System.out.println(siteName);
		if (StringUtils.isBlank(siteTypeList)) {
			return MsgResult.nullResult("请选择要处理的数据!");
		}
		Map<String, Object> region = UserUtil.getRegion();
		String regionCode = (String) region.get("regionCode");  //获取用户的regionCode
		params.put("regionCode", regionCode);
		System.out.println(regionCode);
		try{
			List<Map<String, Object>> siteList = regionSiteService.findSiteInfoBySiteTypes(params);
			return MsgResult.okResult(siteList);
		}catch (Exception e) {
			log.error("站点名称查询", e);
			return MsgResult.exceptionResult();
		}
	}
	
	/**
	 * 返回当前用户能看到所有站点类型的站点的个数  
	 * @author  sjd 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "siteNum", method = RequestMethod.GET)
	public MsgResult findSiteNumSiteType(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> params  = new HashMap<String,Object>();
		Map<String, Object> region = UserUtil.getRegion();
		String regionCode = (String) region.get("regionCode");  //获取用户的regionCode
		params.put("regionCode", regionCode);
		try{
			List<Map<String, Object>> listMap = regionSiteService.findSiteNumSiteType(params);
			return MsgResult.okResult(listMap);
		}catch (Exception e) {
			log.error("返回站点个数出错", e);
			return MsgResult.exceptionResult();
		}
		
	}

	/**
	 * 查询登录用户的各种点位集合
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/findSiteInfoByRegionCodeAndSiteTye", method = RequestMethod.GET)
	public MsgResult findSiteInfoByRegionCodeAndSiteTye(HttpServletRequest request, HttpServletResponse response) {
		/**
		 * 国控点 nationalSite 省控点 provinceSite 小型站 smallAirSite 工业源 industrySite
		 * 扬尘 raiseDustSite 油烟 lampblackSite 喷漆房 sprayBoothSite 所有标准站
		 * allNationalSite 所有空气站 allAirSite 所有污染源 allPollutionSite 所有站点 allSite
		 */
		String pointType = request.getParameter("pointType");
		String searchRegionCode = request.getParameter("searchRegionCode");
		// 点位类型 默认 国控点
		pointType = null == pointType || "".equals(pointType) ? SiteTypeEnum.ALL_NATIONAL_SITE.getValue() : pointType;

		Map<String, Object> region = UserUtil.getRegion();
		String regionCode = (String) region.get("regionCode");

		List<Map<String, Object>> siteInfoList = null;

		try {
			String cityCode = null;
			if (null != searchRegionCode) {
				cityCode = searchRegionCode;
			}else if (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType)
					|| SiteTypeEnum.LAMPBLACK_SITE.getValue().equals(pointType)
					|| SiteTypeEnum.SPRAY_BOOTH_SITE.getValue().equals(pointType)
					|| SiteTypeEnum.RAISE_DUST_SITE.getValue().equals(pointType)) {

				cityCode = regionCode;
			}else{
				/**
				 * 根据当前登录用户区域，查询该区域的上级区域
				 */
				List<Map<String, Object>> parentRegionList = regionSiteService
						.findParentRegionInfoByRegionCode(regionCode);
				// 把登录用户所属省份、和市编码，读取出来
				for (Map<String, Object> parentRegionMap : parentRegionList) {
					String retionType = String.valueOf(parentRegionMap.get("regionType"));
					// 市级名称
					if ("20".equals(retionType)) {
						cityCode = String.valueOf(parentRegionMap.get("regionCode"));
						break;
					}
				}
			}
			
			if (null == cityCode) {
				cityCode = regionCode;
			}
			
//			Map<String, String> paraMap = new HashMap<String, String>();
//
//			paraMap.put("regionCode", cityCode);
//			paraMap.put("siteType", pointType);
			RegionSiteParam siteParam = new RegionSiteParam();
			siteParam.setRegionCode(cityCode);
			siteParam.setSiteType(pointType);
			siteInfoList = regionSiteService.findSiteInfoByRegionCodeAndSiteType(siteParam);
			return MsgResult.okResult(siteInfoList);
		} catch (Exception e) {
			e.printStackTrace();
			return MsgResult.errorResult("查询点位信息出现异常，后台程序异常。");
		}
		
	}


	/**
	 * 根据登陆用户所属区域查询下面的区域及子区域
	 * @author 李亚坤
	 * @createtime 2017年7月18日09:14:06
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findRegionListBySession", method = RequestMethod.GET)
	public MsgResult findRegionListBySession(HttpServletRequest request) {
		try {
			String minLevel = request.getParameter("minLevel");
			Map<String, Object> paramMap = new HashMap<>();
			paramMap.put("minLevel", minLevel);
			return MsgResult.okResult(regionSiteService.findSonRegionInfoByRegionCode(paramMap));
		}catch (Exception e) {
			e.printStackTrace();
			return MsgResult.errorResult("查询登陆用户的区域及子区域异常,后台程序异常.");
		}
	}

}
